<?php
/**
 * Created by PhpStorm.
 * User: asus
 * Date: 2017/8/26
 * Time: 15:01
 */

namespace Wechat\Controller;

/*成为经纪人前台*/
class IdfaceController extends CommonController
{
    /*注册首页*/
    public function index()
    {
        if(IS_POST){
            $post = i('post.');

            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     3145728 ;// 设置附件上传大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =     './data/'; // 设置附件上传根目录
            $upload->savePath  =     'upload/wechat/'; // 设置附件上传（子）目录
            // 上传文件
            $info   =   $upload->upload();

            if(!$info) {// 上传错误提示错误信息
                $this->error($upload->getError());
            }

            $imgs = "";
            foreach($info as $file){
                /*多图上传*/
                $imgs .= $file['savepath'].$file['savename'].'|';
            }

            $idcard = $this->isCreditNo($post['idnum']);
            if(!$idcard){
                $this->error('身份证格式不正确');
            }
            if(trim(empty($post['name']))) {
                $this->error('请填写身份证姓名');
            }
            if(trim(empty($post['idnum']))) {
                $this->error('请填写身份证号');
            }
            if(trim(empty($post['server']))) {
                $this->error('请选择服务地区');
            }

            $data = [
                'name'=>$post['name'],
                'idnum'=>$post['idnum'],
                'namecard'=>$post['namecard'],
                'server'=>$post['server'],
                'idcard'=>$imgs,
                'openid'=>$_SESSION['wechatopenid'],
                'time'=>date("Y年m月d日 H:i:sa",time()),
                'check'=>0,
            ];

            if(M('idcard')->add($data)){
                M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->save(['iszhuce'=>1]);
                $this->success('提交成功，请耐心等待工作人员审核',U('Wechat/Mine/index'));
            }else{
                $this->error('失败，未知错误');
            }
        }else{
            $this->display();
        }
    }
    /*提醒注册为经纪人*/
    public function tixing()
    {
        $user = M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->find();
        $this->assign('data',$user);
        $this->display();
    }
    /*抢单*/
    public function qiangdan()
    {
        $user = M('wechatuser')->where(['openid'=>$_SESSION['wechatopenid']])->find();
        if($user['isjingjiren']!=1){
            $this->redirect('Wechat/Idface/tixing');
        }else{
            $this->redirect('Wechat/Qiangdan/index');
        }
    }
}